//
// Created by wangheng on 2018/7/30.
//

#include "Hello.h"
#include <android/log.h>
#include "my_log.h"
#include <unistd.h>

#include <stdlib.h>

#include <pthread.h>


extern "C"
JNIEXPORT void JNICALL
Java_opensource_xinye_com_test11_jni_Hello_testMemoryWithCpp(JNIEnv *env, jclass type) {
    int* memory = new int;
    if(memory == NULL){
        // 无法分配足够的内存
    }else{
        *memory = 0;
    }
    delete memory;

    long* array = new long[16];
    if(array == NULL){
        // 无法分配足够的内存
    }else{
        array[5] = 100;
    }

    delete[] array;

}


extern "C"
JNIEXPORT void JNICALL
Java_opensource_xinye_com_test11_jni_Hello_testMemory(JNIEnv *env, jclass type) {

    size_t size = 8;
    int* array = static_cast<int *>(malloc(sizeof(int) * size));

    if(array != NULL){
        for(int i = 0;i < size;i++){
            *(array + i) = i;
        }

        free(array);
        array = NULL;
    }else{
        // 分配内存失败,不能分配足够的内存
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_opensource_xinye_com_test11_jni_Hello_testNDKStack(JNIEnv *env, jclass type) {

    getpid();
    getuid();
}

JNIEXPORT void JNICALL
Java_opensource_xinye_com_test11_jni_Hello_hello(JNIEnv *env, jobject instance) {
    env->NewStringUTF("二笔");
    __android_log_print(ANDROID_LOG_INFO,"wangheng","hello");
}

extern "C"
JNIEXPORT jstring JNICALL
Java_opensource_xinye_com_test11_jni_Hello_getStringFromC(JNIEnv *env, jclass type) {

//    jmethodID constructor = env->GetStaticMethodID(type,"<init>",NULL);
//
//    jmethodID method = env->GetMethodID(type,"getStringFromJava",NULL);
//
//    jobject instance = env->NewObject(type,constructor);
//
//    jobject str = env->CallObjectMethod(instance,method);
//    env->


    MY_LOG_VERBOSE("Hello world");
    MY_LOG_INFO("你妹的");
    MY_LOG_ASSERT(3 > 2,"3 > 2 is right")
    MY_LOG_INFO("111Hello WORLD!!!");
    MY_LOG_INFO("current city:%s","Beijing");
    MY_LOG_INFO("Hello WORLD!!!");


    return env->NewStringUTF("那是啥");
}

